<?php
namespace app\common\model;

class Live extends Common
{
    protected $autoWriteTimestamp = true;
    protected $createTime = 'ctime';
    protected $updateTime = 'utime';
    /**
     * 个人主页视频列表
     */
    public function videoList($data){
        switch($data['type']){
            case 1:#他的视频
                $list=$this->alias('a')
                    ->join('zp_user b','a.uid=b.id')
                    ->field('a.*,b.nickname,b.avatar')
                    ->where('uid',$data['uid']);
                $list=$list->page($data['page'],$data['limit'])->order('a.id desc')->select();
                foreach($list as $k=>$v){
                    if(stripos($v['cover'],'_thumb')){
                        $list[$k]['thumb']=$v['cover'];
                    }else{
                        $list[$k]['thumb']=$v['cover'].'_thumb';
                    }
                }
                $total=$list->count();
                break;
            case 2:#喜欢的
                $list=$this->alias('a')
                    ->join('zp_live_zans b','a.id=b.live_id')
                    ->join('zp_user c','a.uid=c.id')
                    ->field('a.*,c.nickname,c.avatar')
                    ->where('b.uid',$data['uid']);
                $list=$list->page($data['page'],$data['limit'])->order('a.id desc')->select();
                foreach($list as $k=>$v){
                    if(stripos($v['cover'],'_thumb')){
                        $list[$k]['thumb']=$v['cover'];
                    }else{
                        $list[$k]['thumb']=$v['cover'].'_thumb';
                    }
                }
                $total=$list->count();
                break;
            case 3:#关注
                $list=$this->alias('a')
                    ->join('zp_live_follow b','a.id=b.live_id')
                    ->join('zp_user c','b.f_uid=c.id')
                    ->field('a.*,c.nickname,c.avatar')
                    ->where('b.uid',$data['uid']);
                $list=$list->page($data['page'],$data['limit'])->order('a.id desc')->select();
                foreach($list as $k=>$v){
                    if(stripos($v['cover'],'_thumb')){
                        $list[$k]['thumb']=$v['cover'];
                    }else{
                        $list[$k]['thumb']=$v['cover'].'_thumb';
                    }
                }
                $total=$list->count();
                break;
        }

        return ['count'=>$total,'list'=>$list];
    }

    /**
     * 获取直播视频
     */
    public function myLive($data){
        $list=$this->where([['uid','=',$data['uid']],['times','>',0],['utime','>',0]]);
        $list=$list->page($data['page'],$data['limit'])->select();
        foreach($list as $k=>$v){
            $list[$k]['utime']=date('m-d H:i',$v['utime']);
            $list[$k]['ctime']=date('m-d H:i',$v['ctime']);
            if(stripos($v['cover'],'_thumb')){
                $list[$k]['thumb']=$v['cover'];
            }else{
                $list[$k]['thumb']=$v['cover'].'_thumb';
            }
        }
        $total=$list->count();
        return ['count'=>$total,'list'=>$list];
    }
    /**
     * 创建直播
     * @param array $data 数据
     *
     */
    public function createLive($data)
    {
        $live=$this->create($data,true);
        if($live){
            $live->stream_name=$data['room_id'].$live->id;
            #$live->play_name=$data['room_id'].$live->id.'-play.mp4';
            $live->utime=0;
            $live->save();
            Room::where('id',$data['room_id'])->update(['is_live'=>1,'utime'=>time()]);
            #更新直播商品
            LiveGoods::where([
                ['uid','=',$data['uid']],
                ['live_id','=',0],
                ['room_id','=',$data['room_id']]
            ])->update(['live_id'=>$live->id]);
        }
        return [true,$live->id];
    }
    /**
     * 直播结束
     */
    public function closeLive($data){
        $live=$this->where(['id'=>$data['live_id'],'uid'=>$data['uid']])->find();
        if($live){
            $utime=time();
            $times=($utime-$live->ctime);
            $minute=ceil($times/60);
            if($minute<2){
                $minute=2;
            }
            #$live->play_name=$live->room_id.$live->id.'-play.mp4';
            #$live->utime=time();
            $live->times=$minute;
            $live->save();
            #更新直播间直播状态
            Room::where('id',$live->room_id)->update(['is_live'=>0,'utime'=>time()]);
            #计算直播时长
            if($minute>60){
                $duration=floor($minute / 60).'hour'.($minute % 60).'min';
            }else{
                $duration=$minute.'min';
            }
            $live['times']=$duration;
            return [true,$live];
        }
        return [false,'获取失败'];
    }
    /**
     * 用户信息表关联
     * @return [type] [description]
     */
    public function user(){
        return $this->hasOne(User::class,'id','uid');
    }
    /**
     * 商品分类表关联
     * @return [type] [description]
     */
    public function goodcat(){
        return $this->hasOne(GoodsCat::class,'id','sort_id');
    }
    /**
     * 区域表关联
     * @return [type] [description]
     */
    public function area(){
        return $this->hasOne(Area::class,'id','city_id');
    }
    /**
     * 时间范围查询
     * @param  [type] $query [description]
     * @param  [type] $value [description]
     * @param  [type] $data  [description]
     * @return [type]        [description]
     */
    public function searchCtimeAttr($query,$value, $data)
    {
        $start=$value[0]?strtotime($value[0]):0;
        $end=$value[1]?strtotime($value[1]):0;
        if(!empty($start)&&!empty($end)){
            $query->where('live.ctime','between',[$start,$end]);
        }elseif(!empty($start)&&empty($end)){
            $query->where('live.ctime','>',$start);
        }elseif(empty($start)&&!empty($end)){
            $query->where('live.ctime','<',$end);
        }
    }
    /**
     * 关键字查询
     * @param  [type] $query [description]
     * @param  [type] $value [description]
     * @param  [type] $data  [description]
     * @return [type]        [description]
     */
    public function searchNameAttr($query,$value, $data)
    {
        if(!empty($value)){
            $query->where('live.uid|live.title',$value);
        }
    }
}
